package liuds.music.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import liuds.music.R;
import liuds.music.model.News;

/**
 * Created by Administrator on 2018/5/23.
 */

public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<News> news;

    private OnItemClickListener onItemClickListener;

    public NewsAdapter(Context context,List<News> news){
        this.context = context;
        this.news = news;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.news_item,null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(v);
                }
            }
        });
        return new NewsHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NewsHolder newsHolder = (NewsHolder)holder;
        News newsItem = news.get(position);
        newsHolder.title.setText(newsItem.getTitle());
        newsHolder.author.setText(newsItem.getAuthor_name());
        newsHolder.date.setText(newsItem.getDate());
        Glide.with(context).load(newsItem.getThumbnail_pic_s()).into(newsHolder.picture);
    }

    @Override
    public int getItemCount() {
        return news.size();
    }

    class NewsHolder extends RecyclerView.ViewHolder{

        private TextView title;
        private TextView author;
        private TextView date;
        private ImageView picture;

        public NewsHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            author = itemView.findViewById(R.id.author);
            date = itemView.findViewById(R.id.date);
            picture = itemView.findViewById(R.id.picture);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(View v);
    }
}
